/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-10-26 13:28:53   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.controller;

import java.util.List;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.Shift;
import com.zx.cloud.util.Page;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.mapper.JsonMapper;
import com.zx.cloud.domain.Dict;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.service.DictService;
import com.zx.cloud.service.LogService;

/**
 * 
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX + "/dict")
public class DictController {

	private static final Logger logger = Logger.getLogger(DictController.class);

	@Autowired
	private DictService dictService;
	
	@Autowired
	private LogService logService;

	@ApiOperation(value = "查询全部字典内容", notes = "查询字典内容")
	@RequestMapping(value = "findDict", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Dict>> findDict(
			@ApiParam(hidden = true, name = "登录用户ID") @CurrentUserId Long userId,
			@RequestBody Dict dict) {
		logService.addLog(userId, "查询全部字典内容", 0);
		logger.info("查询数据库字典: " + JsonMapper.nonDefaultMapper().toJson(dict));
		Page<Dict> datas = dictService.page(dict);
		return new ObjectDataResponse<Page<Dict>>(datas);
	}

	@ApiOperation(value = "修改字典内容", notes = "修改字典内容")
	@RequestMapping(value = "updateDict", method = RequestMethod.POST)
	@ResponseBody
	public Response updateDict(@CurrentUserId Long userId,
			@RequestBody Dict dict) {
		logService.addLog(userId, "修改字典内容", 0);
		logger.info("修改数据库字典: " + JsonMapper.nonDefaultMapper().toJson(dict));
		if(dict.getStatus()==null){
			logger.error("修改字典 前端务必传status");
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS);
		}
		int success = dictService.update(dict);
		return new ObjectDataResponse<Integer>(success);
	}

	@ApiOperation(value = "添加字典内容", notes = "添加字典内容")
	@RequestMapping(value = "addDict", method = RequestMethod.POST)
	@ResponseBody
	public Response addDict(@CurrentUserId Long userId, @RequestBody Dict dict) {
		logService.addLog(userId, "添加字典内容", 0);
		logger.info("添加数据库字典: " + JsonMapper.nonDefaultMapper().toJson(dict));
		int success = dictService.insert(dict);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "查询父类字典内容", notes = "查询字典内容")
	@RequestMapping(value = "queryPageByParent", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Dict>> queryPageByParent(
			@ApiParam(hidden = true, name = "登录用户ID") @CurrentUserId Long userId,
			@RequestBody Dict dict) {
		logService.addLog(userId, "查询父类字典内容", 0);
		logger.info("查询数据库字典: " + JsonMapper.nonDefaultMapper().toJson(dict));
		//得到所有父类
		dict.setStatus(0);
		Page<Dict> datas = dictService.queryPageByParent(dict);
		for(int i=0;i<datas.getData().size();i++){
			//查询父类下所有子类
			Dict dictEntity = new Dict();
			dictEntity.setParentCode(datas.getData().get(i).getCode());
			dictEntity.setStatus(0);
			List<Dict> dictList = dictService.findDictList(dictEntity);
			datas.getData().get(i).setDictSubList(dictList);
		}		
		return new ObjectDataResponse<Page<Dict>>(datas);
	}
}
